<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_User_tournament_entity{
	var $id = 0;
	var $user_id = 0;
	var $tournament_id = 0;
	var $point = 0;
	var $bet_fee_set = ''; 
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_USER_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->user_id = $result[0]->user_id;
			$this->tournament_id = $result[0]->tournament_id;
			$this->point = $result[0]->point;
			$this->bet_fee_set = $result[0]->bet_fee_set;
			
			return $this;
		}
		return false;
	}
	
	function setUserId($input){ $this->user_id=$input; }
	function setTournamentId($input){ $this->tournament_id=$input;}
	function setPoint($input){ $this->point=$input; }
	function setBetFeeSet($input){ $this->bet_fee_set=$input; }
	
	function getId(){ return $this->id; }
	function getUserId(){ return $this->user_id; }
	function getTournamentId(){ return $this->tournament_id; }
	function getPoint(){ return $this->point; }
	function getBetFeeSet(){ return $this->bet_fee_set; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('user_id', $this->user_id);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('point', $this->point);
		$ci->db->set('bet_fee_set', $this->bet_fee_set);
		
		$ci->db->where('id', $this->id)->update(TBL_USER_TOURNAMENT);
	}
	
	function createNewUserTournamentEntity(){
		$ci=& get_instance();
		$ci->db->set('user_id', 0);
		$ci->db->insert(TBL_USER_TOURNAMENT);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfTournament($tournament_id){
		$ci=& get_instance();
		return $ci->db->where( "tournament_id = $tournament_id" )->order_by('point','desc')->get(TBL_USER_TOURNAMENT)->result();
	}
	
	function findUserInTournament($user_id, $tournament_id){
		$tmp = null;
		foreach($this->getArrayListOfTournament($tournament_id) as $item){
			if($item->user_id == $user_id){
				$tmp = $item;
				break;
			}
		}
		return $tmp;
	}
	
	function isUserSetBetFee($user_id, $tournament_id){
		$returnRS = 'no';
		$item = $this->findUserInTournament($user_id, $tournament_id);
		if($item){
			if($item->bet_fee_set == 'yes'){
				$returnRS = 'yes';
			}
		}
		return $returnRS;
	}
	
	function setUserBetFeeDeduct($user_id, $tournament_id, $value){
		$ci=& get_instance();
		$item = $this->findUserInTournament($user_id, $tournament_id);
		if($item){
			$ci->db->set('bet_fee_set', $value);
			$ci->db->where('id', $item->id)->update(TBL_USER_TOURNAMENT);
		}else{
			$newid = $this->createNewUserTournamentEntity();
			$ci->db->set('user_id', $user_id);
			$ci->db->set('tournament_id', $tournament_id);
			$ci->db->set('bet_fee_set', $value);
			$ci->db->where('id', $newid)->update(TBL_USER_TOURNAMENT);
		}
	}
	
	function myRankInTournament($user_id, $tournament_id){
		$tmp = 1;
		$find = false;
		foreach($this->getArrayListOfTournament($tournament_id) as $item){
			if($item->user_id == $user_id && $item->point){
				$find = true;
				break;
			}
			$tmp++;
		}
		if($find)
			return $tmp;
		else
			return null;
	}
	
	function getMyPointInTournament($user_id, $tournament_id){
		$tmp = 0;
		$find = false;
		foreach($this->getArrayListOfTournament($tournament_id) as $item){
			if($item->user_id == $user_id && $item->point){
				$find = true;
				$tmp = $item->point;
				break;
			}
		}
		if($find)
			return $tmp;
		else
			return null;
	}
	
	function myCoinsStakedInTournament($user_id, $tournament_id){
		$ci=& get_instance();
		$result = $ci->db->where(array('user_id'=>$user_id, 'tournament_id'=>$tournament_id))
							->get(TBL_BET)->result();
		$coins = 0;
		foreach($result as $item){
			if($item->status != 'cancelled'){
				$coins += $item->stake;
			}
		}
		$result2 = $ci->db->where(array('user_id'=>$user_id, 'tournament_id'=>$tournament_id))
							->get(TBL_BET_MANUAL)->result();
		foreach($result2 as $item){
			if($item->status != 'cancelled'){
				$coins += $item->stake;
			}
		}
		$result3 = $ci->db->where(array('user_id'=>$user_id, 'tournament_id'=>$tournament_id))
							->get(TBL_BET_TOURNAMENT)->result();
		foreach($result3 as $item){
			if($item->status != 'cancelled'){
				$coins += $item->stake;
			}
		}
		$result4 = $ci->db->where(array('user_id'=>$user_id, 'tournament_id'=>$tournament_id))
							->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		foreach($result4 as $item){
			if($item->status != 'cancelled'){
				$coins += $item->stake;
			}
		}
		return $coins;
	}
	
	function getMyTournament($user_id){
		$ci=& get_instance();
		return $ci->db->where('user_id', $user_id)->order_by('id','desc')->get(TBL_USER_TOURNAMENT)->result();
	}
	
	function findUserHasARankInTournament($rank , $tournament_id){
		$ci=& get_instance();
		$tmp = 1;
		foreach($this->getArrayListOfTournament($tournament_id) as $item){
			if($tmp == $rank){
				return ($ci->user_entity->init('id', $item->user_id));//clone_
				break;
			}
			$tmp++;
		}
		return null;
	}
	
	function reCalTournamentRank($tournament_id){
		$ci=& get_instance();
		//$TOURNAMENT = $ci->tournament_entity->init("id", $tournament_id);
		
		$strQR = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_MANUAL." 
									WHERE tournament_id=$tournament_id AND result='won'
									GROUP BY user_id ORDER BY total DESC"; //
		$res1 = $ci->db->query($strQR )->result();
		$res_rs1 = array();
		foreach($res1 as $k=>$v){
			$res_rs1[$v->user_id] = $v->total;
		}
			
		$strQR2 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET." 
									WHERE tournament_id=$tournament_id AND result='won'
									GROUP BY user_id ORDER BY total DESC"; //
		$res2 = $ci->db->query($strQR2 )->result();
		$res_rs2 = array();
		foreach($res2 as $k=>$v){
			$res_rs2[$v->user_id] = $v->total;
		}
		
		$strQR3 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_TOURNAMENT." 
									WHERE tournament_id=$tournament_id AND result='won'
									GROUP BY user_id ORDER BY total DESC"; //
		$res3 = $ci->db->query($strQR3 )->result();
		$res_rs3 = array();
		foreach($res3 as $k=>$v){
			$res_rs3[$v->user_id] = $v->total;
		}
		
		$strQR4 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_MANUAL_TOURNAMENT." 
									WHERE tournament_id=$tournament_id AND result='won' 
									GROUP BY user_id ORDER BY total DESC"; //
		$res4 = $ci->db->query($strQR4 )->result();
		$res_rs4 = array();
		foreach($res4 as $k=>$v){
			$res_rs4[$v->user_id] = $v->total;
		}
		
	/*//////////////////////////////////////////////////////////////////////////////*/
		$ranksArray = array();
		foreach($res_rs1 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs2 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs3 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs4 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		
		$str = "SELECT user_id, sum(point) as total FROM ".TBL_CONVERSION." 
						WHERE tournament_id=$tournament_id
						GROUP BY user_id ";
		$convertWeek = $ci->db->query($str)->result();
		foreach($convertWeek as $k=>$v){
			if(isset($ranksArray[$v->user_id])){
				$ranksArray[$v->user_id] = $ranksArray[$v->user_id] - $v->total;
			}
		}
		
		foreach($ranksArray as $k=>$v){
			if($v <=0){
				unset($ranksArray[$k]);
			}
		}
		
		asort($ranksArray);
		$ranksArray = array_reverse($ranksArray,true);
		
		foreach($ranksArray as $user_id=>$point){
			$ci->db->where(array('tournament_id'=>$tournament_id,'user_id'=>$user_id))
						->update(TBL_USER_TOURNAMENT,array('point'=>$point));
		}
	}
	//endclass
}
